# Copyright (c) 2020, Mopon.php
# All rights reserved.
# 文件名称： mapFrame.php
#wxpython tab切换页面  https://blog.csdn.net/weixin_30730053/article/details/96541681?utm_medium=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-1.test_version_3&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-1.test_version_
__author__ = "lishang"
__date__ = "$2021-7-2 11:30:51$"
import os,time
import wx,wx.html,wx.html2
import picmap

class mapFrame(wx.Frame):
    def __init__(self,layout=''):
        self.layout=layout
        wx.Frame.__init__(self,None,-1,"照片地图",size=(350, 100))
        self.all_SizerItem=[]
        self.panel=wx.Panel(self,-1)#主panel
        self.hox=wx.BoxSizer(wx.VERTICAL)#垂直布局
#        self.create_button()#第一行button行
#        self.create_tap_item()#第二行item行
#        self.create_web_view()
        self.panel.SetSizer(self.hox)
        self.hox.Layout()
#        self.create_Menu()
        self.create_Toolbar()
        self.setStatus("启动成功")
        self.Center() 
        
    def setStatus(self,msg=''):
        if not hasattr(self,'statusBar'): 
           self.statusBar=self.CreateStatusBar()
           self.statusBar.SetFieldsCount(2) # 状态栏分成3个区域
           self.statusBar.SetStatusWidths([-9,-1]) #区域宽度比列，用负数
#        if msg:   
##            wx.Timer(5000, self.setStatus, '')
#            wx.FutureCall(5000, self.setStatus, '')
        self.statusBar.SetStatusText(msg) #给状态栏设文字
        
    def create_Toolbar(self):
        toolbar = self.CreateToolBar()
        realpath=os.path.dirname(os.path.realpath(__file__))
        # wx.NewId(),"配置,user-order,配置路径".split(',')
        # map(toolbar.AddTool,)
        i=0; names=list({'配置':'配置路径','重置':'清空所有数据','关于':'版本信息','浏览器打开':'在浏览器中打开'}.items())
        for  name in  'user-order,shop-car,start-sobot,share'.split(',') : 
            icon=wx.Bitmap("%s/pic/%s.png" % (realpath,name))
            toolbar.AddTool(wx.NewId(), names[i][0],icon,icon,wx.ITEM_NORMAL,names[i][1])
            i+=1
        # edit = toolbar.AddSimpleTool(wx.NewId(), wx.Bitmap("%s/pic/shop-car.png" % realpath), "重置", "清空所有数据")
        self.Bind(wx.EVT_TOOL,self.onToolClick)
        toolbar.Realize()

    def onToolClick(self,event):
            print(event.GetId())
            if event.GetId() ==100:
                dialog=self.layout.Get('pathList')
                dialog.ShowModal()
            elif event.GetId()==101:
                if self.layout.confirm("确定要重置所有数据吗，此过程不可逆！") == wx.ID_YES: 
                        print ("del all data do:")
                        try:
                            picmap.db.init()
                            self.layout.alert('操作成功！')
                        except Exception as e:
                            self.layout.alert(str(e),'重置失败',wx.ICON_ERROR)
            elif event.GetId()==102:
                    self.layout.alert("照片地图展示by keli@202107.sz ",'关于',wx.ICON_ERROR)
            elif event.GetId()==103:
                webbrowser.open(self.indexPage);
#            dialog.Show(True)
        
    def create_web_view(self):
            child_panel = wx.Panel(self.panel, -1)#新建子panel
            panel_child_hox=wx.BoxSizer(wx.HORIZONTAL)
#            html = wx.html.HtmlWindow(child_panel)
#            text="1234"
#            filePath="./picmap/html/bmap.html"
#            file =open(filePath,'r',encoding='utf8')
#            text=file.read()
#            html.SetPage(text)
            # html.SetBackgroundColour(wx.RED)
            w=webWindow(self,1)
#            html = wx.html2.WebView.New()
            html = wx.html2.WebView.New(self)
            html.LoadUrl(self.indexPage)

            self.browser=html
            panel_child_hox.Add(html,1,wx.EXPAND|wx.ALL,0)
            child_panel.SetSizer(panel_child_hox)
            self.hox.Add(child_panel,1,wx.EXPAND|wx.ALL,20)#把item下的子panel添加Sizer

    # def onButtonClick(self,event):
    #     label=wx.FindWindowById(event.GetId()).GetLabelText()
    #     for each,item in self.all_SizerItem:
    #         if each!=label:
    #             self.hox.Hide(item)#如果不是button label显示的item隐藏
    #         else:
    #             check_item=item
    #     self.hox.Show(check_item)
    #     self.panel.Layout()#重新布局            



    # def create_tap_item(self):
    #     button_item=["hello","the","world"]
    #     for i,each in enumerate(button_item):
    #         child_panel = wx.Panel(self.panel, -1)#新建子panel
    #         panel_child_hox=wx.BoxSizer(wx.HORIZONTAL)
    #         html = wx.html.HtmlWindow(child_panel)
    #         text="1234"
    #         html.SetPage(text)
    #         # html.SetBackgroundColour(wx.RED)
    #         panel_child_hox.Add(html,1,wx.EXPAND|wx.ALL,0)
    #         child_panel.SetSizer(panel_child_hox)
    #         self.hox.Add(child_panel,1,wx.EXPAND|wx.ALL,20)#把item下的子panel添加Sizer
    #         if i!=0:
    #             self.hox.Hide(child_panel)#隐藏不是第一列的item
    #         self.all_SizerItem.append((each,child_panel))#将所有的item下的子panel添加进列表
            
    # def create_Menu(self):
    #     menuBar = wx.MenuBar()
    #     menu1 = wx.Menu()
    #     menuItem = menu1.Append(-1, "&Quit", "&Quit")
    #     menuItem1 = menu1.Append(-1, "&Quit1", "&Quit1")
    #     menuBar.Append(menu1, "&File")
    #     self.SetMenuBar(menuBar)
        
    # def create_button(self):
    #     child_panel = wx.Panel(self.panel, -1)#创建一个子panel
    #     self.hox_button = wx.BoxSizer(wx.HORIZONTAL)  # 按钮水平布局
    #     button = wx.Button(child_panel, -1, "hello")
    #     button1 = wx.Button(child_panel, -1, "the")
    #     button2 = wx.Button(child_panel, -1, "world")
    #     self.hox_button.Add(button, proportion=0, flag=wx.ALL, border=0)
    #     self.hox_button.Add(button1, proportion=0, flag=wx.ALL, border=0)
    #     self.hox_button.Add(button2, proportion=0, flag=wx.ALL, border=0)
    #     child_panel.SetSizer(self.hox_button)
    #     self.Bind(wx.EVT_BUTTON, self.onButtonClick)
    #     self.hox.Add(child_panel,0,wx.EXPAND)#把新建的子panel添加Size
class webWindow(wx.Window):
        def __init(self,parent,id):
            wx.Window.__init__(self,parent,id)
            